﻿namespace Vampirewal.Core.WPF.Theme;

/// <summary>
/// ShowDialogWindow.xaml 的交互逻辑
/// </summary>
[VampirewalIoCRegister("ShowDialogWindow", RegisterType.Transient)]
public partial class ShowDialogWindow : WindowBase, IDialogWindow
{
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="Setting"></param>
    public ShowDialogWindow()
    {
        InitializeComponent();
    }

    public void Setting(DialogWindowSetting setting)
    {
        if (setting.UcView == null)
        {
            throw new Exception("DialogWindow窗体页面不能为null");
        }

        SetValue(setting);
    }

    private void SetValue(DialogWindowSetting Setting)
    {
        this.SetValue(Window.WidthProperty, Setting.WindowWidth);
        this.SetValue(Window.HeightProperty, Setting.WindowHeight);

        UcView.Content = Setting.UcView;

        Setting.UcView.Width = double.NaN;
        Setting.UcView.Height = double.NaN;

        this.SetValue(Window.DataContextProperty, Setting.UcView.DataContext);

        ViewModelBase vm = Setting.UcView.DataContext as ViewModelBase;
        //vm.View = this;
        vm.ViewId = this.ViewId;

        vm.PassData(Setting.PassData);

        if (!string.IsNullOrEmpty(Setting.IconStr))
        {
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new System.Uri(Setting.IconStr);
            bitmap.EndInit();

            //this.Icon = new BitmapImage(new Uri(Setting.IconStr));
            this.Icon = bitmap;
            this.ShowInTaskbar = true;
        }
        else
        {
            this.ShowInTaskbar = false;
        }

        this.IsShowMaxButton = Setting.IsShowMaxButton;
        this.IsShowMinButton = Setting.IsShowMinButton;
        this.IsOpenWindowSize = Setting.IsOpenWindowSize;

        if (!string.IsNullOrEmpty(Setting.Background))
        {
            this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.Background));
        }
        else
        {
            this.Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#2F3437"));
        }

        if (!string.IsNullOrEmpty(Setting.Foreground))
        {
            this.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.Foreground));
        }
        else
        {
            this.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#EBEBEB"));
        }

        if (!string.IsNullOrEmpty(Setting.WindowBorderBrush))
        {
            this.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Setting.WindowBorderBrush));
        }
        else
        {
            this.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#3F4447"));
        }

        this.TitleFontSize = Setting.TitleFontSize;

        if (Setting.CloseWindowCommand != null)
        {
            this.CloseWindowCommand = Setting.CloseWindowCommand;
            this.CloseWindowCommandParameter = Setting.CloseWindowCommandParameter;
        }

        if(Setting.CloseDialogWindowCallBackCommand != null)
        {
            //this.SetValue(CloseDialogWindowCallBackCommandProperty, Setting.CloseDialogWindowCallBackCommand);

            this.CloseDialogWindowCallBackCommand= Setting.CloseDialogWindowCallBackCommand;
            this.CloseDialogWindowCallBackCommandParameter= Setting.CloseDialogWindowCallBackCommandParameter;
        }
    }

    protected override void CloseWin_Click(object sender, RoutedEventArgs e)
    {
        CloseDialogWindowCallBackCommand?.Execute(CloseDialogWindowCallBackCommandParameter);

        base.CloseWin_Click(sender, e);
    }

    

    /// <summary>
    /// 弹窗关闭的命令
    /// </summary>
    public ICommand CloseDialogWindowCallBackCommand { get; set; }


    public static readonly DependencyProperty CloseDialogWindowCallBackCommandProperty =
        DependencyProperty.Register("CloseDialogWindowCallBackCommand", typeof(ICommand), typeof(ShowDialogWindow), new PropertyMetadata(null));



    /// <summary>
    /// 弹窗关闭的命令参数
    /// </summary>
    public object CloseDialogWindowCallBackCommandParameter
    {
        get { return (object)GetValue(CloseDialogWindowCallBackCommandParameterProperty); }
        set { SetValue(CloseDialogWindowCallBackCommandParameterProperty, value); }
    }

    // Using a DependencyProperty as the backing store for CloseDialogWindowCallBack.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty CloseDialogWindowCallBackCommandParameterProperty =
        DependencyProperty.Register("CloseDialogWindowCallBackCommandParameter", typeof(object), typeof(ShowDialogWindow), new PropertyMetadata(null));


}